﻿using Manon.Core.Extensions;
using Manon.Gateway.Admin.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Manon.Gateway.Admin.Controllers
{

    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        public Task<List<Menu>> GetMenu()
        {
            string filePath = Directory.GetCurrentDirectory().Replace("\\", "/");

            List<Menu> menus = JsonHelper.Read<List<Menu>>($@"{filePath}/Config/menu.json");

            if (menus == null) return null;


            //过滤权限 
            List<Menu> newMenus = new List<Menu>();
            foreach (var item in menus)
            {

                List<Menu> newMenusChild = new List<Menu>();
                if (item.children != null)
                {
                    foreach (var child in item.children)
                    {
                        child.children = null;
                        newMenusChild.Add(child);
                    }
                }

                item.children = newMenusChild;
                newMenus.Add(item);
            }

            return Task.FromResult(newMenus);
        }




    }
}
